#!/usr/bin/env python
# encoding: utf-8
"""
putio-test.py

Created by aft on 2010-02-19.
Copyright (c) 2010 Put.io. All rights reserved.


"""

import putio
import sys

if __name__ == '__main__':

    # connecting your put.io with your api key and api secret
    api = putio.Api(api_key="XXXXXX",api_secret="XXXXXX")
    
    # is authorized?
    if api.is_ready(): 
        print "Success"
    
    # get user info
    me = api.get_user_info()
    print me.__dict__
    
    print "Available bandwidth : ", putio.human_size(me.bw_quota_available)
    print "Total bandwidth     : ", putio.human_size(me.bw_quota)
    print "Available Percantage: %" + "%.2f" % (float(me.bw_quota_available)/float(me.bw_quota)*100.00)
    
    
    #################################
    print "*" * 20
    print "my files"
    print "*" * 20, "\n\n"
    #################################
    
    
    # getting your items
    items = api.get_items(limit=2, offset=0)
    for it in items:
        print "%5s  %s" % (it.id, it.name)
        
    
    # creating a folder
    newitem = api.create_folder(name="blabla")
    
    # create a folder in another folder
    subfolder = newitem.create_folder(name="this is a sample subfolder")
    
    # getting one item
    item = api.get_items(id=newitem.id)[0]
    #item = api.get_items()[0]
    
    # getting an item info
    item = item.update_info()
    print item.name, item.id, item.is_dir
    if item.is_dir is True: print "%s is a folder." % item.name
        
    # renaming an item
    print "old name: %s" % item.name
    newitem = item.rename_item("renamed by api")
    print "new name: %s" % newitem.name
    
    # moving an item to a target folder
    # 0 being root of your files
    subfolder.move_item(target=0)
       
    # deleting an item
    newitem.delete_item()
    subfolder.delete_item()
    
    # searching items
    sresults = api.search_items("avi from:me type:video")
    
    print "*" * 20
    print "search"
    print "*" * 20, "\n\n"
    
    for sr in sresults:
        print sr.name
    
    # getting stream urls of video files
    for sr in sresults:
        print sr.get_stream_url()
    
    
    #################################
    print "*" * 20
    print "friends"
    print "*" * 20, "\n\n"
    #################################
    
    friends = api.get_friends()
    if friends:
        for f in friends:
            print friends.index(f), f.name
        
        # get friends' files
        a_friend = friends[1]
        fitems = a_friend.get_items(limit=5)
        
        if fitems:
            print a_friend.name, " has these items:"
            for f in fitems: print "    -", f.name
    
    

    
    #################################
    print "*" * 20
    print "dashboard"
    print "*" * 20, "\n\n"
    #################################
    
    
    # getting your dashboard messages
    msgs = api.get_messages()
    
    print "*" * 20
    print "messages"
    print "*" * 20, "\n\n"
    
    for m in msgs:
        print "* %s" % m.title
    
    
    #################################
    print "*" * 20
    print "subscriptions"
    print "*" * 20, "\n\n"
    #################################
    
    
    # getting your subscriptions
    subsc = api.get_subscriptions()
    
    for s in subsc:
        print " - %s" % s.name
    
    
    # creating a subscription
    newsub = api.create_subscription(name="created by api", url="http://a.b.c")
    print newsub.__dict__
    if newsub: print "%s created." % newsub.name
    
    
    # adding a filter to a subscription
    
    # editing a subscription
    newsub.edit(title="renamed by api", url="http://www.pilli.com/rss.xml")
    
    # updating a subscription info
    newsub = newsub.update_info()
    
    # toggling subscription activity
    newsub = newsub.toggle_status()
    print "Info: %s" % newsub.__dict__
    
    # toggling subscription activity, again.
    newsub = newsub.toggle_status()
    print "Info: %s" % newsub.__dict__
    
    # add/subtracting a filters
    newsub = newsub.add_do_filters("hdtv","avi",101)
    print "Edited subs info: %s" % newsub.__dict__
    
    newsub = newsub.add_dont_filters("720p","dvdscreener")
    print "Edited subs info: %s" % newsub.__dict__
    
    newsub = newsub.del_do_filters(101)
    print "Edited subs info: %s" % newsub.__dict__
    
    newsub = newsub.del_dont_filters("dvdscreener")
    print "Edited subs info: %s" % newsub.__dict__
    
    # deleting subscriptions
    newsub.delete()    
    
    
    
    #################################
    print "*" * 20
    print "url analyzation and fetching"
    print "*" * 20, "\n\n"
    #################################
    
        
    txt  = """
       ftp://ftp1.mplayerhq.hu/MPlayer/samples/MPEG-4/mp3on4/id5_1.mp4
         
     """
    
    # creating an url bucket
    print "\nCreating bucket"
    bucket = api.create_bucket()
    
    # extracting urls from text
    print "\nExtracting urls"
    eurl = bucket.extract_urls(txt)
    print eurl
        
    # creating an url bucket
    print "\nAnalyzing"
    report = bucket.analyze(eurl)
    print "Analyzation Results:", report
    
    # fetch the bucket
    print "\nFetching"
    transfers = bucket.fetch()
    
    if transfers:
        for t in transfers: print t.name, t.status, t.percent_done, "\n"
    
    # getting the report
    print "\nReport:"
    report = bucket.get_report()
    print report
    
    
    
    #################################
    print "*" * 20
    print "transfers"
    print "*" * 20, "\n\n"
    #################################
    
    
    # getting all active transfers
    trans = api.get_transfers()
    
    if newtransfers:
        for t in newtransfers: print t.name, t.status, t.percent_done
    else: print "you have no active transfers"
    
    # canceling transfers
    for t in trans:
        print "canceling", t.name 
        t.destroy_transfer()
    

    sys.exit(0)

